package com.fingard.crypto;


import cn.topca.api.cert.CertApiException;
import cn.topca.api.cert.TCA;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class ConfigTool {
    private static ConfigTool configTool = new ConfigTool();
    private boolean initialized = false;
    private ConfigTool(){}

    public static ConfigTool getInstance(){
        return configTool;
    }

    public void init( String configFilePath) throws IOException, CertApiException {
        if (initialized)
            return;
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(new File(configFilePath));
            byte[] buf = new byte[fis.available()];
            fis.read(buf);
            fis.close();
            fis = null;
            String json = new String(buf, "UTF-8");
            TCA.config(json);
            initialized = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(fis != null){
                    fis.close();
                }
            }catch(Exception ex){
                System.out.println(ex.toString());
            }
        }
    }
}